package com.ccl.iot.device;

import android.graphics.Bitmap;

import com.ccl.iot.IOTNetAPIEnum;
import com.ccl.iot.batch.IOTBatch;
import com.ccl.iot.object.IOTDeviceInfoEx;
import com.ccl.iot.dvr.utils.Consts;
import com.ccl.iot.managers.ImageManager;

/**
 * 添加到意向控制的批处理
 */
public class BatchIntentionDevice extends IOTDevice{
	
	private IOTBatch mBat = null;
	private String mUdid = null;

	public BatchIntentionDevice(long aDevID, IOTDeviceInfoEx aDeviceInfo, int aRegAddress, int aRegSize) {
		super(aDevID, aDeviceInfo, aRegAddress, aRegSize);
		this.setOnline(true);
		this.setAuthState(IOTNetAPIEnum.EIOT_NETWORK_TYPE.EIOT_NETWORK_TYPE_REMOTE, IOTNetAPIEnum.EAUTH_STATE.EAUTH_STATE_OWNER);
	}
	
	public BatchIntentionDevice(IOTBatch batch) {
		this(batch.getID(), null, -1, -1);
		this.mBat = batch;
	}

	@Override
	public byte[] getRegister() {
		return null;
	}

	@Override
	public void updateRegister(int aAddress, int aSize, byte[] aData, int aDataPos, int aNetWork, long time) {
		
	}

	@Override
	public String getStateID() {
		return null;
	}

	@Override
	public String[] getStateIDs() {
		return null;
	}
	
	@Override
	public String getName() {
			return mBat == null?null:mBat.getName();
	}
	
	@Override
	public boolean setName(String aName) {

		if(mBat != null && aName != null){
			aName = aName + Consts.BATCH_SPLIT+mBat.getRawImageIndex();
			if(!aName.equals(mBat.getNameAndImgIndex())){
				return mBat.setName(aName);
			}else{
				return true;
			}
		}
		return false;
	}
	
	public IOTBatch getBatch(){
		return mBat;
	}


	@Override
	public boolean setImage(Bitmap aImage) {
		String tID = getImageID(Consts.BATCH);

		getImageCount();

		if (aImage == null) {
			if (ImageManager.RemoveBitmap(tID)) {
				return true;
			}
		} else {

			ImageManager.SetBitmap(tID, aImage);
			return true;
		}

		return false;
	}

	@Override
	public Bitmap getImage() {

		String tID = getImageID(Consts.BATCH);
		Bitmap tBitmap = ImageManager.GetBitmap(tID);
		return tBitmap;

	}

	@Override
	public int getImageCount() {

		if (ImageManager.ContainsBitmap(getImageID(Consts.BATCH))) {
			return 1;
		}
		return 0;
	}

	@Override
	public String getUDID() {
		if(mUdid == null){
			mUdid = "B"+mBat.getID();
		}
		return mUdid;
	}
}
